/*
 * ========THE SOLMIX PROJECT=====================================
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * http://www.gnu.org/licenses/ 
 * or see the FSF site: http://www.fsf.org. 
 */
package org.solmix.fmk.repo;

import java.util.List;

import org.solmix.api.exception.SLXException;
import org.solmix.api.repo.DSRepoService;
import org.solmix.api.repo.IDSRepo;
import org.solmix.commons.util.DataUtil;

/**
 * 
 * @version $Id$
 */
public class DSRepoManager implements DSRepoService
{
   private DSRepoTracker tracker;

   private IDSRepo defaultRepo;

   /**
    * @return the defaultRepo
    */
   public IDSRepo getDefaultRepo()
   {
      return defaultRepo;
   }

   /**
    * @param defaultRepo
    *           the defaultRepo to set
    */
   public void setDefaultRepo(IDSRepo defaultRepo)
   {
      this.defaultRepo = defaultRepo;
   }

   /**
    * @return the tracker
    */
   public DSRepoTracker getTracker() throws SLXException
   {
      return tracker;
   }

   /**
    * @param tracker
    *           the tracker to set
    */
   public void setTracker(DSRepoTracker tracker)
   {
      this.tracker = tracker;
   }

   /**
    * {@inheritDoc}
    * 
    * @see org.solmix.api.repo.DSRepoService#loadDSRepo()
    */
   @Override
   public IDSRepo loadDSRepo(String ID) throws SLXException
   {
      if ("default".equals(ID) || ID == null) {
         return this.defaultRepo;
      } else {
         List<IDSRepo> repos = this.tracker.getRepos();
         for (IDSRepo repo : repos) {
            if (repo.getName().equals(ID)) {
               return repo;
            }
         }
         return null;
      }
   }

   /**
    * {@inheritDoc}
    * 
    * @see org.solmix.api.repo.DSRepoService#getRepos()
    */
   @Override
   public IDSRepo[] getRepos() throws Exception
   {
      IDSRepo[] rps;

      List<IDSRepo> repos = this.tracker.getRepos();
      if (repos != null && !repos.isEmpty())
      return (IDSRepo[]) DataUtil.listToArray(tracker.getRepos());
      else {
         IDSRepo res[] = {defaultRepo};
         return res;
      }
   }

}
